package com.ogawa.chair7808.ui.activity

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.jeremyliao.liveeventbus.LiveEventBus
import com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_END_TIME
import com.ogawa.base.utils.CommonUtil
import com.ogawa.chair7808.R
import com.ogawa.chair7808.interfaces.IProMasaChoose
import com.ogawa.chair7808.manager.DataManager
import com.ogawa.chair7808.utils.Activity7808Utils
import com.ogawa.chair7808.view.VerticalViewPager.VerticalAdapter
import com.ogawa.chair7808.view.VerticalViewPager.VerticalViewPager
import com.ogawa.chair7808.widget.*
import java.util.*

/**
 * 专业按摩
 */
class ProfessionalMassageActivity7808 : BaseMqtt7808Activity(), View.OnClickListener, IProMasaChoose //        , IProfessionalMassageView7506
{
//    private var mPageztms: LayoutZtms_7506? = null
//    private var mPageqjsd: LayoutQjsd_7506? = null
//    private var mPagerqzs: LayoutRqzs_7506? = null
//    private var mPagegzsy: LayoutGzsy_7506? = null
//    private var mPageshfs: LayoutShfs_7506? = null
    private var shfs:Layout7808Shfs?=null
    private var rqzs:Layout7808Rqzs?=null
    private var bwam:Layout7808Bwam?=null
    private var ztms:Layout7808Ztms?=null
    private var mAdapter: VerticalAdapter? = null
    private var mVerticalViewPager: VerticalViewPager? = null
    private var mVerticalViews: MutableList<View?>? = null
    private var mIvPointOne: ImageView? = null
    private var mIvPointTwo: ImageView? = null
    private var mIvPointThree: ImageView? = null
    private var mIvPointFour: ImageView? = null
    private var mTvNameOne: TextView? = null
    private var mTvNameTwo: TextView? = null
    private var mTvNameThree: TextView? = null
    private var mTvNameFour: TextView? = null
    private val mPoints: MutableList<ImageView?> = ArrayList()
    private val mClassifys: MutableList<TextView?> = ArrayList()

    //    private ProfessionalMassagePresenter7506 mPresenter;
    private var mCurName: String? = null
//    private var mCurCode: String? = null
    override fun initTitleBar() {
        super.initTitleBar()
        titleBar.setTitleNameGone()
    }

    override fun initView(layoutId: Int) {
        super.initView(layoutId)
//        mCurCode = DataManager.inst.auto
        shfs = Layout7808Shfs(this)
        rqzs = Layout7808Rqzs(this)
        bwam = Layout7808Bwam(this)
        ztms = Layout7808Ztms(this)
        mTvNameOne = findViewById(R.id.tv_name1)
        mTvNameTwo = findViewById(R.id.tv_name2)
        mTvNameThree = findViewById(R.id.tv_name3)
        mTvNameFour = findViewById(R.id.tv_name4)
        mTvNameOne?.setOnClickListener(this)
        mTvNameTwo?.setOnClickListener(this)
        mTvNameThree?.setOnClickListener(this)
        mTvNameFour?.setOnClickListener(this)
        mIvPointOne = findViewById(R.id.pm_potin_one)
        mIvPointOne?.setSelected(true)
        mTvNameOne?.setSelected(true)
        mIvPointTwo = findViewById(R.id.pm_potin_two)
        mIvPointThree = findViewById(R.id.pm_potin_three)
        mIvPointFour = findViewById(R.id.pm_potin_four)
        mPoints.clear()
        mPoints.add(mIvPointOne)
        mPoints.add(mIvPointTwo)
        mPoints.add(mIvPointThree)
        mPoints.add(mIvPointFour)
        mClassifys.clear()
        mClassifys.add(mTvNameOne)
        mClassifys.add(mTvNameTwo)
        mClassifys.add(mTvNameThree)
        mClassifys.add(mTvNameFour)
        mVerticalViewPager = findViewById(R.id.verticalViewPager)
        mVerticalViews = ArrayList()
        mVerticalViews?.add(shfs)
        mVerticalViews?.add(rqzs)
        mVerticalViews?.add(bwam)
        mVerticalViews?.add(ztms)
//        mVerticalViews?.add(mPagegzsy)
        mVerticalViewPager?.setOnPageChangeListener(OnPageChangeListenerImpl())
        mAdapter = VerticalAdapter(mVerticalViews)
        mVerticalViewPager?.adapter = mAdapter
        //        mPresenter = new ProfessionalMassagePresenter7506(this);
//        mPresenter.initData();


        LiveEventBus.get(LIVE_EVENT_BUS_KEY_END_TIME, Int::class.java).observe(this, {
            if (CommonUtil.isActivityTop(ProfessionalMassageActivity7808::class.java, this)) {
                showFinishDialog(this, "0") {
                    cancleFinishDialog()
                    Activity7808Utils.startHomeActivity(this@ProfessionalMassageActivity7808)
                    finish()
                }
            }
        })

    }

    override fun chooseMassage(name: String, Code: String) {
        DataManager.inst.sendCommond(Code)
//        if (TextUtils.isEmpty(mCurName) || mCurName != name) {
//        } else {
//            Activity7808Utils.startProMasDetailActivity(this, name)
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //        mPresenter.onDestory();
    }

    //    @Override
    //    public void noticeChange(int page, int position) {
    //        makaChoose(page, position);
    //    }
    //
    //    @Override
    //    public void closeActivity() {
    //        CloseProfessionalObserver.getInst().noticeonClose();
    //    }
    //
//        @Override
//        public void startAdvance() {
//            if(CommonUtil.isActivityTop(ProfessionalMassageActivity7506.class,this)){
//                ActivityUtils.startAdvancedMassageActivity(this);
//                this.finish();
//            }
//
//        }
    //
    //    @Override
    //    public void startDetail(String name) {
    //        if(CommonUtil.isActivityTop(ProfessionalMassageActivity7506.class,this)){
    //            ActivityUtils.startProMasDetailActivity(this,name);
    //        }
    //    }
    override fun setLayoutId(): Int {
        return R.layout.activity_professional_massage_7808
    }

    //    @Override
    //    public void getName(String name) {
    //        mCurName = name;
    //    }
//    override fun timeFinish() {
//        super.timeFinish()
//        if (CommonUtil.isActivityTop(ProfessionalMassageActivity7808::class.java, this)) {
//            showFinishDialog(this, "0") {
//                cancleFinishDialog()
//                Activity7808Utils.startHomeActivity(this@ProfessionalMassageActivity7808)
//                finish()
//            }
//        }
//    }

    //    @Override
    //    public void isPause(boolean isPause) {
    //        if(isPause){
    //            pauseTime();
    //        }
    //        else {
    //            reStarTime();
    //        }
    //    }
    internal inner class OnPageChangeListenerImpl : VerticalViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float,
                                    positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            for (i in mPoints.indices) {
                if (position == i) {
                    mPoints[i]!!.isSelected = true
                    mClassifys[i]!!.isSelected = true
                } else {
                    mPoints[i]!!.isSelected = false
                    mClassifys[i]!!.isSelected = false
                }
            }
        }

        override fun onPageScrollStateChanged(state: Int) {}
    }

    override fun onClick(v: View) {
        val i = v.id
        if (i == R.id.tv_name1) {
            mVerticalViewPager!!.currentItem = 0
        } else if (i == R.id.tv_name2) {
            mVerticalViewPager!!.currentItem = 1
        } else if (i == R.id.tv_name3) {
            mVerticalViewPager!!.currentItem = 2
        } else if (i == R.id.tv_name4) {
            mVerticalViewPager!!.currentItem = 3
        }
    }


    override fun changeViewByMqttMessage() {
        shfs?.setSelect()
        rqzs?.setSelect()
        bwam?.setSelect()
        ztms?.setSelect()

    }
}